import {
    wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        devId: '',
        imgUrl: app.globalData.imgUrl,
        device: {},
        typeIndex: 0,
        deviceList: [],
        typeId: '',
        uploadUrl: wx.getStorageSync('uploadUrl'),
        stoId: ''
    },

    onShow: function (options) {
        this.setData({
            typeId: wx.getStorageSync('currentTypeId')
        })
        this.getDeviceList()

    },

    onLoad: function (options) {
        console.log(options,12312312321231231);
        if (options.id) {
            this.data.stoId = options.id
            this.dyeRecord();
        }
        if (options.devId) {
            this.setData({
                devId: options.devId
            })
            return
        }
        if (wx.getStorageSync('devId')) {
            this.setData({
                devId: wx.getStorageSync('devId')
            })
            return
        }
    },

    goToReplace() {
        wx.showModal({
            title: '提示',
            content: '是否要去换料？',
            success(res) {
                if (res.confirm) {
                    let devId = wx.getStorageSync('devId')
                    if (devId) {
                        wx.setStorageSync('devId', devId)
                    }
                    wx.navigateTo({
                        url: '/pages/color_replace_do/index',
                    })
                }
            }
        })
    },

    //下拉刷新
    onPullDownRefresh: function () {
        this.refreshData();
        wx.stopPullDownRefresh()
    },

    refreshData: function () {
        this.setData({
            pageNo: 1
        })
        this.getDeviceList()
    },


    //获取记录列表
    dyeRecord: function () {
        let params = {
            pageNo: 1,
            pageSize: 50,
            keyword: this.data.keyword,
            stoId: this.data.stoId
        }
        if(this.data.deviceList.length && this.data.deviceList[this.data.typeIndex].id) {
            params.devId = this.data.deviceList[this.data.typeIndex].id
        }

        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + '/gds/protected/dyeRecord/findDyeList',
            method: 'POST',
            data: params
        }, (res) => {
            let list = res.list;
            this.setData({
                recodeList: list,
            });

            console.log(res)

        })
    },

    //触底时，加载更多
    onReachBottom: function () {
        if (this.data.hasNextPage) {
            this.setData({
                pageNo: this.data.pageNo + 1
            });
            this.dyeRecord();
        }
    },

    //搜索关键词
    seachInputConfirm: function (e) {
        this.setData({
            pageNo: 1,
            keyword: e.detail.value
        })
        this.dyeRecord();
    },

    seachBtn: function () {
        this.setData({
            pageNo: 1,
            keyword: this.data.keyword
        })
        this.dyeRecord();
    },

    seachInputVal: function (e) {
        var inputval = e.detail.value;
        console.log(e)
        this.setData({
            keyword: inputval
        })
    },

    //清空
    clearInput: function () {
        this.setData({
            keyword: ''
        })
        this.dyeRecord();
    },

    //详情
    toDetail: function (e) {
        let userInfo = wx.getStorageSync('userInfo');
        if (userInfo && userInfo.orgType != 3 && userInfo.orgType != 1) {
            return;
        }
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/pages/color_residue/detail?id=' + item.id,
        })
    },

    getDeviceList: function () {
        var params = {
            keyword: this.data.keyword,
            pageNo: 1,
            typeId: this.data.typeId,
            pageSize: 999,
            stoId: this.data.stoId
        }

        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + '/gds/protected/device/findList',
            method: 'POST',
            data: params
        }, (res) => {
            let list = res.list.map(item => {
                if (item.stoName) {
                    item.note += ` - ${item.stoName}`;
                }

                if (item.stoCreateName) {
                    item.note += ` - ${item.stoCreateName}`;
                }
                return item;
            });
            let typeIndex = list.findIndex(item => item.id == this.data.devId);
            if (typeIndex == -1) {
                typeIndex = 0;
            }
            console.log(typeIndex);
            this.setData({
                deviceList: list,
                typeIndex
            });
        })
    },




})